using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data.Common;
using System.Xml;
using MacomberMap.Common.Types;

namespace MacomberMap.Common.Components
{
    /// <summary>
    /// This class holds information on a connectivity node, containing elements to which it is connected
    /// </summary>
    [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.Both)]
    public class MM_Node: MM_Element
    {
        #region Variable declarations
        /// <summary>The elements to which the node is connected</summary>
        private MM_Element[] _ConnectedElements;

        /// <summary>The elements to which the node is connected</summary>
        [Category("Data"),Description("The elements to which the node is connected"), MM_Serializable_Target(MM_Serializable_Target.enumTargetType.TypeCollection)]
        public MM_Element[] ConnectedElements
        {
            get { return _ConnectedElements; }
            set { _ConnectedElements = value; }
        }

        /// <summary>The busbar section corresponding to the node</summary>
        private MM_BusbarSection _BusbarSection = null;

        /// <summary>
        /// The busbar section corresponding to the node
        /// </summary>
        [Category("Data"), Description("The busbar section corresponding to the node")]
        public MM_BusbarSection BusbarSection
        {
            get { return _BusbarSection; }
            set { _BusbarSection = value; }
        }


        /// <summary>The EB corresponding to the node</summary>
        private MM_ElectricalBus _ElectricalBus = null;

        /// <summary>
        /// The EB corresponding to the node
        /// </summary>
        [Category("Data"), Description("The EB corresponding to the node"), MM_Serializable_Target(MM_Serializable_Target.enumTargetType.TypeCollection)]
        public MM_ElectricalBus ElectricalBus
        {
            get { return _ElectricalBus; }
            set { _ElectricalBus = value; }
        }
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new node from a data reader
        /// </summary>
        /// <param name="ElementSource"></param>
        public MM_Node(DbDataReader ElementSource) : base(ElementSource) 
        {
            String[] splStr = ((string)ElementSource["ConnectedElements"]).Split(new char[] { ','}, StringSplitOptions.RemoveEmptyEntries);
            ConnectedElements = new MM_Element[ splStr.Length];
            for (int a = 0; a < splStr.Length; a++)
                ConnectedElements[a] = (MM_Element)MM_Serializable.RetrieveConvertedValue(typeof(MM_Element), splStr[a]);
        }

        /// <summary>
        /// Initialize a new node from an XML document
        /// </summary>
        /// <param name="ElementSource"></param>
        public MM_Node(XmlElement ElementSource) : base(ElementSource) 
        {
            String[] splStr = (ElementSource.Attributes["ConnectedElements"]).Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            ConnectedElements = new MM_Element[splStr.Length];
            for (int a = 0; a < splStr.Length; a++)
                ConnectedElements[a] = (MM_Element)MM_Serializable.RetrieveConvertedValue(typeof(MM_Element), splStr[a]);
        }

        /// <summary>
        /// Initialize a parameterless node
        /// </summary>
        public MM_Node()
            : base()
        { }         
        #endregion
    }
}
